<?php
//
// Article Class
// The SDMworld! Network
//

uiLoadLibrary('page');

class clsForumPost extends cDycmsObject
{
    protected $mClsId = 'ForumPost';

	protected $mForum;
	protected $mParentPost;
	
	const allowedTags = '<a><b><em><i><br><font><style>';
	
	function Render(cDycmsClientNode &$r_client, $disp = true)
	{
		if($this->Exists()){
			//
			// Query all top level replies
			// TODO: full threading
			//
			
			$obj = new cDycmsObjectQuery('ForumPost');
			$obj->AddArray(array(
				'forum' => $this->mAttributes['forum'],
				'replyTo' => $this->mId));
			$n = $obj->Execute();
			
			
			//
			// Create the 'New Topic' and 'Reply' buttons
			//
			
			$btn = new cUiForm(array(
				'action' => dyEditorUrl('ForumPost'),
				'method' => 'POST',
				'id' => 'Forum-'.$this->mId));
			$btn->AddChild(new cUiFormData('forum',
				$this->mAttributes['forum']));
			$btn->AddChild(new cUiFormData('title',
				'Re: '.$this->mAttributes['title']));
			$btn->AddChild(new cUiButtonSet(array(
				'name' => 'replyTo'), array(
				0 => uiGetIcon('page_add').'New Topic',
				$this->mId => uiGetIcon('page_add').'Post Reply'
			)));
		
		}else{
			$btn = new cDycmsClientNode();
			$n = 0;
		};

		
		//
		// Format date
		//
		
		if(date('d', $this->mAttributes['date']) == date('d'))
			$date = 'Today '.date('h:ma T',$this->mAttributes['date']);
		else
			$date = date("d M Y h:ma T",$this->mAttributes['date']);
		
		
		//
		// Create the table
		//
		
		$r_client->AddChild($hdr = new cUiLayer('ContentTitle'));
		$hdr->AddChild($this->mAttributes['title']);
		
		$r_client->AddChild($body = new cUiLayer('ContentBody'));
		
		$body->AddChild($btn);
		$body->AddChild($tbl = new cUiTable(array(
			'Author', 'Message'), array(
			'class' => 'ForumPost'
		)));
		$body->AddChild($btn);
		
		
		$tbl->AddChild($row = new cUiTableRow());
			$row->AddChild($this->mOwner->GetUserName());
			$row->AddChild(new cUiWikiRenderer(cUiWikiRenderer::$zDefaultToks,
				$this->mAttributes['text'], self::allowedTags));
		
		for($i = 0; $i < $n; $i++){
			$tbl->AddChild($row = new cUiTableRow(array(2)));
			$row->AddChild($obj->GetSummary($i));
			
			$tbl->AddChild($row = new cUiTableRow());
			$row->AddChild($obj->GetOwner($i)->GetUserName());
			$row->AddChild(new cUiWikiRenderer(cUiWikiRenderer::$zDefaultToks,
				$obj->Get('text', $i), self::allowedTags));
		}
	}

	function DoEdit($vzAttrib){
		
		//
		// Get the forum ID, check that it exists and that we have
		// access to it.
		//
		
		$forum = (isset($vzAttrib['forum']) ? $vzAttrib['forum']
				: $this->mAttributes['forum']);
		
		$this->mForum = dyLoadObject('Forum', $forum);
		if(!$this->mForum->Exists()){
			header('Location: '.dyViewerUrl('Forum'));
			throw new dyExNotFound('Invalid forum ID - cannot find forum');
		};
		
		$this->mForum->Validate(S_VIEW);
		
		
		//
		// Perform check on the thread this is in reply to. If the
		// thread is locked or non existent, throw an error. Note that
		// we allow the replyTo to be zero (for top-level threads).
		//
		
		$replyTo = (isset($vzAttrib['replyTo']) ? $vzAttrib['replyTo']
				: $this->mAttributes['replyTo']);
		
		$this->mParentPost = dyLoadObject('ForumPost', $replyTo);
		
		if($this->mParentPost->Exists()){
			
			// TODO: thread locking
			

		}else{
			
			//
			// Clear the title if we havent posted any text.
			//
			
			if(!isset($vzAttrib['text']))
				unset($vzAttrib['title']);
		}
		
		return parent::DoEdit($vzAttrib);
	}
	
	function DrawEditor(cUiEditor &$editor)
	{
		//
		// Draw the editor form
		//

		$editor->AddChild(new cUiFormData('replyTo',
			$this->mAttributes['replyTo']));
		$editor->AddChild(new cUiFormData('forum',
			$this->mAttributes['forum']));
			
		$editor->AddChild($titleField = new cUiFormItem('Post Title'));
		$titleField->AddChild(new cUiTextBox(array(
			'name' => 'title',
			'value' => $this->mAttributes['title']
		), 'Post Title'));
		
		if($this->mParentPost && $this->mParentPost->Exists()){
			$titleField->AddChild("In Reply To: ");
			$titleField->AddChild(new cHtmlHyperlink(
				dyViewerUrl('ForumPost', $this->mParentPost->GetId()),
				$this->mParentPost->GetSummary()));
		}else{
			$titleField->AddChild("In Forum: ");
			$titleField->AddChild(new cHtmlHyperlink(
				dyViewerUrl('Forum', $this->mForum->GetId()),
				$this->mForum->GetSummary()));
		};
		
		$editor->AddChild(new cUiEditorTextArea('text',
			$this->mAttributes['text'], null));
		
		//
		// Add buttons
		//
		
		$editor->AddChild(new cUiButtonSet(array(
				'name' => '$$os-editorAction'
			), $this->mId == 0 ? array(
				'save' => uiGetIcon('page_go').'Post',
				'preview' => uiGetIcon('magnifier').'Preview'
			) : array(
				'save' => uiGetIcon('page_save').'Save Post',
				'preview' => uiGetIcon('magnifier').'Preview',
				'view' => uiGetIcon('accept').'View'
		)));
	}
	
	function DrawPublisher(cUiPublisher &$rPublisher)
	{
		throw new dyExInvalidOp("Forum posts cannot be published");
	}
	
	function DoPublish()
	{
		throw new dyExInvalidOp("Forum posts cannot be published");
	}
	
	function GetSummary()
	{
    	return $this->mAttributes['title'];
	}

	function LoadObject()
	{
        $this->mForum =& dyLoadObject('Forum', $this->mAttributes['forum']);
	}

    function Create(){
        $this->mAttributes['text'] = '';
		$this->mAttributes['title'] = '';
		$this->mAttributes['replyTo'] = 0;
		$this->mAttributes['forum'] = 0;
		$this->mAttributes['views'] = 0;
		$this->mAttributes['date'] = time();
		$this->mParentPost = null;
    }
}

class cCollectionArticle extends cDycmsAbstractCollection
{
	function GetDisplay()
	{
		$buf = "";
		foreach($this->mObject as $obj)
		{
			$buf .= "<div class='article'>\n";
			$buf .= $obj->GetDisplay(NULL, false);
			$buf .= "</div>\n";
		};
		return $buf;
	}

	public function GetSummary($i = -1)
	{
		if($i > -1)
			return $this->mObject[$i]->GetSummary();

		$vz = $this->GetUniqueValues('site');
		$n = count($vz);
		if($n != 1)
			return "Articles";
		$buf = "/ {$vz[0]}";

		$vz = $this->GetUniqueValues('category');
		$n = count($vz);
		if($n != 1)
			return $buf;
		$buf .= " / {$vz[0]}";
		return $buf;
	}
}

?>
